package com.hboxs.asl.controller.app.member;

import com.hboxs.asl.*;
import com.hboxs.asl.controller.app.BaseController;
import com.hboxs.asl.entity.*;
import com.hboxs.asl.service.*;
import com.hboxs.asl.util.ListUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * Created by xxx on 2016/11/25.
 * Controller -- 个人资料
 */
@Controller(value = "appProfileController")
@RequestMapping(value = "/app/m/profile")
public class ProfileControl extends BaseController {

    protected Logger logger = Logger.getLogger(getClass().getName());

    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "fileServiceImpl")
    private FileService fileService;
    @Resource(name = "giveawayServiceImpl")
    private GiveawayService giveawayService;
    @Resource(name = "memberCertificationServiceImpl")
    private MemberCertificationService memberCertificationService;
    @Resource(name = "caseServiceImpl")
    private CaseService caseService;
    @Resource(name = "categoryServiceImpl")
    private CategoryService categoryService;
    @Resource(name = "verificationCodeServiceImpl")
    private VerificationCodeService verificationCodeService;
    @Resource(name = "financialRecordServiceImpl")
    private FinancialRecordService financialRecordService;

    /**
     * 设置背景图片
     * @param background
     * @return
     */
    @RequestMapping(value = "/background", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage background(String imageType, MultipartFile background) {
        try {
            Member member = memberService.getCurrentByToken();
            if (member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            // 1. 需要FileType
            FileInfo.FileType fileType = FileInfo.FileType.image;
            if (!fileService.isValid(fileType, background)) {
                return JsonMessage.error("上传图片大小或格式不正确");
            }
            // 2. 需要ImageType
            ProductImage.imageType type = ProductImage.imageType.defaultImage;
            if (StringUtils.isNotEmpty(imageType)) {
                try {
                    type = ProductImage.imageType.valueOf(imageType);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            // 3. 调用fileService upload 方法
            String url = fileService.upload(fileType, background, false, type.toString());
            // 4. 返回图片URL
            if (url == null) {
                return JsonMessage.error("图片上传失败！");
            } else {
                member.setBackground(url);
                memberService.update(member);
                return JsonMessage.success(url);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return JsonMessage.UNKNOWN_ERROR;
    }


    @RequestMapping(value = "/personal", method = RequestMethod.GET)
    public
    @ResponseBody
    Object Personal() {
        Member member = memberService.getCurrentByToken();
        if (member == null) {
            return JsonMessage.USER_NOT_LOGIN;
        }
        return JsonMessage.success(
                "ID", member.getID(),
                "avator", member.getAvator(),
                "name", member.getName(),
                "myFansNumber", member.getFavorMembers().size(),
                "point", member.getPoint(),
                "myIncome", member.getEarnings(),
                "balance", member.getBalance()
        );
    }

    /**
     * 修改昵称
     * @param name
     * @return
     */
    @RequestMapping(value = "/edit_nickname", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage editNickName(String name) {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            if (name != null) {
                member.setName(name);
            }
            memberService.update(member);
            return JsonMessage.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 修改简介
     * @param introduction
     * @return
     */
    @RequestMapping(value = "/edit_introduction", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage editIntroduction(String introduction) {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            if(introduction != null) {
                member.setIntroduction(introduction);
            }
            memberService.update(member);
            return JsonMessage.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 上传案例
     * @param example
     * @return
     */
    @RequestMapping(value = "/upload_case", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage uploadCase(Case example) {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            example.setMember(member);
//            System.out.println(example.getContent());
            caseService.save(example);
            return JsonMessage.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 回答金额设置
     * @param memberCertification
     * @return
     */
    @RequestMapping(value = "/settingAmount", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage settingAmount(MemberCertification memberCertification) {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            memberCertification.setId(member.getMemberCertification().getId());
            memberCertificationService.update(memberCertification, "gender", "cerType", "relName", "wechatName", "idCard", "relAddress",
                    "relPhone", "email", "speciality", "faction", "member", "tel", "qualificationImages", "agencyImages", "status");
            member.setMemberCertification(memberCertification);
            memberService.update(member);
            return JsonMessage.success("设置成功");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 获取金额设置
     * @return
     */
    @RequestMapping(value = "/getSettingAmount", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage getSettingAmount() {
        try {
            Member member = memberService.getCurrentByToken();
            if(member != null) {
                MemberCertification memberCertification = member.getMemberCertification();
                return JsonMessage.success(JsonMessage.optimiseObj(memberCertification, "payFengshui", "payGanqing", "payShiye", "payOther", "id"));
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }


    /**
     * 进入修改个人资料
     *
     * @return json
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage editMember() {
        try {
            Member member = memberService.getCurrentByToken();
            if (member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            return JsonMessage.success(JsonMessage.optimiseObj(member, "avator", "name", "introduction", "tags"));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 我的案例
     * @return
     */
    @RequestMapping(value = "/cases", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage cases(Pageable pageable) {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            Page<Case> page = caseService.fingCasePage(pageable, member);
            return JsonMessage.success(

                    "totalPage", page.getTotalPages(),

                    "pageNumber", page.getPageNumber(),

                    "pageSize", page.getPageSize(),

                    "total", page.getTotal(),

                    "url", Constant.ESSAY_URL,

                    "content", JsonMessage.optimiseObj(
                            page.getContent(),
                            "title", "imageURL", "content", "createDateF", "id", "contentWithoutTag"
                    )

            );
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 案例详情
     * @param caseId
     * @return
     */
    @RequestMapping(value = "/case_detail", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage case_detail(Long caseId, HttpServletRequest request) {
        try {
            Member teacher = memberService.getCurrentByToken();
            if(teacher == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            String path = request.getRequestURL().toString();
            String regex = request.getServletPath();
            int index = path.lastIndexOf(regex);
            String contentPath = path.substring(0, index);
            String url =  contentPath + "/app/teacher/caseWeb.htm?id="+caseId;
            return JsonMessage.success(url);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }




    /**
     * 删除案例
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteCase", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage deleteCase(Long id) {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            caseService.delete(id);
            return JsonMessage.success("删除成功");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 修改标签
     * @param tags
     * @return
     */
    @RequestMapping(value = "/updateTags", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage updateTags(String tags) {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            member.setTags(tags);
            memberService.update(member);
            return JsonMessage.success("修改成功");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 个人中心首页
     *
     * @return JsonMessage
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage index(Pageable pageable) {
        try {
            Member teacher = memberService.getCurrentByToken();
            if (teacher == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            Page<Case> page = caseService.fingCasePage(pageable, teacher);
            return JsonMessage.success(
                    "teacher", JsonMessage.optimiseObj(teacher, "avator", "name", "introduction", "tags", "memberCertification.qualificationImages"),

                    "totalPage", page.getTotalPages(),

                    "pageNumber", page.getPageNumber(),

                    "pageSize", page.getPageSize(),

                    "total", page.getTotal(),

                    "url", Constant.ESSAY_URL,

                    "content", JsonMessage.optimiseObj(
                            page.getContent(),
                            "title", "imageURL", "content", "createDateF", "id", "contentWithoutTag"
                    )

            );
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 上传头像
     *
     * @param imageType 图片类型
     * @param avator      图片文件
     * @return 图片链接
     */
    @RequestMapping(value = "/avator", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage uploadImage(String imageType, MultipartFile avator) {
        try {
            Member member = memberService.getCurrentByToken();
            if (member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            // 1. 需要FileType
            FileInfo.FileType fileType = FileInfo.FileType.image;
            if (!fileService.isValid(fileType, avator)) {
                return JsonMessage.error("上传图片大小或格式不正确");
            }
            // 2. 需要ImageType
            ProductImage.imageType type = ProductImage.imageType.defaultImage;
            if (StringUtils.isNotEmpty(imageType)) {
                try {
                    type = ProductImage.imageType.valueOf(imageType);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            // 3. 调用fileService upload 方法
            String url = fileService.upload(fileType, avator, false, type.toString());
            // 4. 返回图片URL
            if (url == null) {
                return JsonMessage.error("图片上传失败！");
            } else {
                    member.setAvator(url);
                    memberService.update(member);
                    return JsonMessage.success(url);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 老师认证(android)
     * @return
     */
    @RequestMapping(value = "/teacher_certification_android", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage teacher_certification_android(MemberCertification memberCertification, String[] imageURL, Long categoryId, String captcha) {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            if(member.getType() != null) {
                if(member.getType().equals(Member.Type.teacher)) {
                    return JsonMessage.error("您已经认证过了");
                }
            }
            List<Filter> filters = new ArrayList<>();
            filters.add(Filter.eq("phone", memberCertification.getTel()));
            List<VerificationCode> codes = verificationCodeService.findList(1, filters, null);
            String sendCode = null;
            if (codes != null) {
                for (VerificationCode verificationCode : codes) {
                    sendCode = verificationCode.getCode();
                }
            }
            if (!captcha.equalsIgnoreCase(sendCode)) {
                return JsonMessage.error("手机验证码不正确");
            }
            Category category = categoryService.find(categoryId);
            if(category != null) {
                member.setCategory(category);
            }
            if(memberCertification.getPayGanqing() == null) {
                memberCertification.setPayGanqing(BigDecimal.ZERO);
            }
            if(memberCertification.getPayShiye() == null) {
                memberCertification.setPayShiye(BigDecimal.ZERO);
            }
            if(memberCertification.getPayFengshui() == null) {
                memberCertification.setPayFengshui(BigDecimal.ZERO);
            }
            if(memberCertification.getPayOther() == null) {
                memberCertification.setPayOther(BigDecimal.ZERO);
            }
            //审核中
            memberCertification.setStatus(MemberCertification.Status.reviewing);
            memberCertification.setCerType(MemberCertification.CerType.person);
            if(imageURL != null) {
                List<String> imageList = Arrays.asList(imageURL);
                memberCertification.setQualificationImages(imageList);
            }
            if (member.getMemberCertification() == null) {
                memberCertification.setMember(member);
                memberCertificationService.save(memberCertification);
            } else {
                memberCertification.setId(member.getMemberCertification().getId());
                memberCertificationService.update(memberCertification, "member", "agencyImages");
            }
            return JsonMessage.success("提交成功");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }


    /**
     * 老师认证(ios)
     * @return
     */
    @RequestMapping(value = "/teacher_certification", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage teacher_certification(MemberCertification memberCertification, String imageURL, String captcha, Long categoryId) {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            if(member.getType() != null) {
                if(member.getType().equals(Member.Type.teacher)) {
                    return JsonMessage.error("您已经认证过了");
                }
            }
            List<Filter> filters = new ArrayList<>();
            filters.add(Filter.eq("phone", memberCertification.getTel()));
            List<VerificationCode> codes = verificationCodeService.findList(1, filters, null);
            String sendCode = null;
            if (codes != null) {
                for (VerificationCode verificationCode : codes) {
                    sendCode = verificationCode.getCode();
                }
            }
            if (!captcha.equalsIgnoreCase(sendCode)) {
                return JsonMessage.error("手机验证码不正确");
            }
            Category category = categoryService.find(categoryId);
            if(category != null) {
                member.setCategory(category);
            }
            //审核中
            memberCertification.setStatus(MemberCertification.Status.reviewing);
            memberCertification.setCerType(MemberCertification.CerType.person);
            if(imageURL != null) {
                List<String> imageList = ListUtils.string2List(imageURL);
                memberCertification.setQualificationImages(imageList);
            }
            if (member.getMemberCertification() == null) {
                memberCertification.setMember(member);
                memberCertificationService.save(memberCertification);
            } else {
                memberCertification.setId(member.getMemberCertification().getId());
                memberCertificationService.update(memberCertification, "member", "agencyImages");
            }
            return JsonMessage.success("提交成功");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 机构认证(android)
     * @return
     */
    @RequestMapping(value = "/institutional_accreditation_android", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage institutional_accreditation_android(MemberCertification memberCertification, String[] qualification_images, String[] business_images, Long categoryId, String captcha) {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            if(member.getType() != null) {
                if(member.getType().equals(Member.Type.teacher)) {
                    return JsonMessage.error("您已经认证过了");
                }
            }
            List<Filter> filters = new ArrayList<>();
            filters.add(Filter.eq("phone", memberCertification.getTel()));
            List<VerificationCode> codes = verificationCodeService.findList(1, filters, null);
            String sendCode = null;
            if (codes != null) {
                for (VerificationCode verificationCode : codes) {
                    sendCode = verificationCode.getCode();
                }
            }
            if (!captcha.equalsIgnoreCase(sendCode)) {
                return JsonMessage.error("手机验证码不正确");
            }
            Category category = categoryService.find(categoryId);
            if(category != null) {
                member.setCategory(category);
            }
            if(qualification_images != null) {
                memberCertification.setQualificationImages(Arrays.asList(qualification_images));
            }
            if(business_images != null) {

                memberCertification.setAgencyImages(Arrays.asList(business_images));
            }
            //审核中
            memberCertification.setStatus(MemberCertification.Status.reviewing);
            memberCertification.setCerType(MemberCertification.CerType.person);
            if (member.getMemberCertification() == null) {
                memberCertification.setMember(member);
                memberCertificationService.save(memberCertification);
            } else {
                memberCertification.setId(member.getMemberCertification().getId());
                memberCertificationService.update(memberCertification, "member");
            }
            return JsonMessage.success("提交成功");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 机构认证(ios)
     * @return
     */
    @RequestMapping(value = "/institutional_accreditation", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage institutional_accreditation(MemberCertification memberCertification, String qualification_images, String business_images, Long categoryId, String captcha) {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            if(member.getType() != null) {
                if(member.getType().equals(Member.Type.teacher)) {
                    return JsonMessage.error("您已经认证过了");
                }
            }
            List<Filter> filters = new ArrayList<>();
            filters.add(Filter.eq("phone", memberCertification.getTel()));
            List<VerificationCode> codes = verificationCodeService.findList(1, filters, null);
            String sendCode = null;
            if (codes != null) {
                for (VerificationCode verificationCode : codes) {
                    sendCode = verificationCode.getCode();
                }
            }
            if (!captcha.equalsIgnoreCase(sendCode)) {
                return JsonMessage.error("手机验证码不正确");
            }
            Category category = categoryService.find(categoryId);
            if(category != null) {
                member.setCategory(category);
            }
            if(qualification_images != null) {
                memberCertification.setQualificationImages(ListUtils.string2List(qualification_images));
            }
            if(business_images != null) {
                memberCertification.setAgencyImages(ListUtils.string2List(business_images));
            }
            //审核中
            memberCertification.setStatus(MemberCertification.Status.reviewing);
            memberCertification.setCerType(MemberCertification.CerType.person);
            if (member.getMemberCertification() == null) {
                memberCertification.setMember(member);
                memberCertificationService.save(memberCertification);
            } else {
                memberCertificationService.update(memberCertification, "member");
            }
            member.setMemberCertification(memberCertification);
            memberService.update(member);
            return JsonMessage.success("提交成功");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 我的证书
     * @return
     */
    @RequestMapping(value = "/my_certifications", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage myCertifications() {
        try {
            Member teacher = memberService.getCurrentByToken();
            if(teacher == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            return JsonMessage.success(
                    JsonMessage.optimiseObj(teacher, "memberCertification.qualificationImages"));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 保存证书安卓
     * @param imageURL
     * @return
     */
    @RequestMapping(value = "/save_images", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage saveImages(String[] imageURL) {
        try {
            Member teacher = memberService.getCurrentByToken();
            if(teacher == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            MemberCertification memberCertification = teacher.getMemberCertification();
            List<String> imageList = Arrays.asList(imageURL);
            memberCertification.setQualificationImages(imageList);
            teacher.setMemberCertification(memberCertification);
            memberService.update(teacher);
            return JsonMessage.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }


    /**
     * 保存证书ios
     * @param imageURL
     * @return
     */
    @RequestMapping(value = "/save_images_ios", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage saveImages(String imageURL) {
        try {
            Member teacher = memberService.getCurrentByToken();
            if(teacher == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            MemberCertification memberCertification = teacher.getMemberCertification();
            if(imageURL != null) {
                List<String> imageList = ListUtils.string2List(imageURL);
                memberCertification.setQualificationImages(imageList);
            }
            teacher.setMemberCertification(memberCertification);
            memberService.update(teacher);
            return JsonMessage.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 获取个人信息
     * @return
     */
    @RequestMapping(value = "/personal_data", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage personalData() {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            MemberCertification.Status status = null;
            if(member.getMemberCertification() != null) {
                if(member.getMemberCertification().getStatus() != null) {
                    status = member.getMemberCertification().getStatus();
                }
            }
            return JsonMessage.success(
                    "id", member.getId()
                    , "ID", member.getID()
                    , "type", member.getType()
                    , "token", member.getToken()
                    , "tokenExpire", member.getTokenExpire()
                    , "name", member.getName()
                    , "avator", member.getAvator()
                    , "myFansNumber", member.getFavorMembers().size()
                    , "point", member.getPoint()
                    , "balance", member.getBalance()
                    , "myIncome", member.getEarnings()
                    , "status", status
                    , "background", member.getBackground()
            );
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 关于我们
     * @return
     */
    @RequestMapping(value = "/about_us", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage aboutUs(HttpServletRequest request) {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            String path = request.getRequestURL().toString();
            String regex = request.getServletPath();
            int index = path.lastIndexOf(regex);
            String contentPath = path.substring(0, index);
            String url =  contentPath + "/app/member/about.htm";
            return JsonMessage.success(url);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 判断用户状态
     * @return
     */
    @RequestMapping(value = "/check_memberType", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage check_memberType() {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            if(member.getType().equals(Member.Type.member)) {
                if(MemberCertification.Status.reviewing.equals(member.getMemberCertification().getStatus())) {
                    return JsonMessage.success("reviewing");
                } else if(MemberCertification.Status.passed.equals(member.getMemberCertification().getStatus())) {
                    return JsonMessage.success("passed");
                } else if(MemberCertification.Status.reject.equals(member.getMemberCertification().getStatus())) {
                    return JsonMessage.success("reject");
                } else {
                    return JsonMessage.success("generalUser");
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 获取所有分类
     * @return
     */
    @RequestMapping(value = "/categories", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage categories() {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            List<Category> categories = categoryService.findAll();
            if(categories.size() > 0) {
                return JsonMessage.success(JsonMessage.optimiseObj(categories, "name", "id", "imageURL"));
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 我的粉丝
     * @return
     */
    @RequestMapping(value = "/getFavorMembers", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage getFavorMembers(Pageable pageable) {
        Member teacher = memberService.getCurrentByToken();
        if (teacher == null) {
            return JsonMessage.USER_NOT_LOGIN;
        }

        Page<Member> page = memberService.findPageFans(pageable, teacher);

        return JsonMessage.success(
                "totalPage", page.getTotalPages(),
                "pageNumber", page.getPageNumber(),
                "pageSize", page.getPageSize(),
                "total", page.getTotal(),
                "content", JsonMessage.optimiseObj(page.getContent(), "avator", "name", "type"));
    }

    /**
     * 我的收入明细
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/financial_record", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage financialRecord(Pageable pageable) {
        Member member = memberService.getCurrentByToken();
        if (member == null) {
            return JsonMessage.USER_NOT_LOGIN;
        }
        Page<FinancialRecord> page = financialRecordService.incomeRecord(pageable, member);
        return JsonMessage.success(
                "totalPage", page.getTotalPages(),

                "pageNumber", page.getPageNumber(),

                "pageSize", page.getPageSize(),

                "total", page.getTotal(),

                "content", JsonMessage.optimiseObj(
                        page.getContent(),
                        "amount", "sn", "member.name", "member.avator", "sourse", "createDateF"
                )
        );
    }


    /**
     * 充值页面
     *
     * @return json
     */
    @RequestMapping(value = "/to_charge", method = RequestMethod.GET)
    public
    @ResponseBody
    Object toCharge() {

        try {
            Member member = memberService.getCurrentByToken();
            if (member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            List<Giveaway> giveaways = giveawayService.findValid();
            return JsonMessage.success(
                    JsonMessage.optimiseObj(
                            giveaways, "title", "quota", "startDate", "endDate", "product.id", "product.name", "product.image", "product.price"
                    )
            );

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }


    /**
     * 检查我的密码是否正确
     */
    @RequestMapping(value = "/check_password", method = RequestMethod.GET)
    public
    @ResponseBody
    Object check_Password(String password) {

        Member member = memberService.getCurrentByToken();
        if (member == null) {
            return JsonMessage.USER_NOT_LOGIN;
        }

        if (StringUtils.isEmpty(password) || !password.equals(member.getPassword())) {
            return JsonMessage.error("密码不正确");
        }

        return JsonMessage.success("密码正确");

    }



}
